﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;
using System.Configuration;
using System.Xml;
using System.Diagnostics;

namespace JKZP
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            try
            {
                PP.gUser = new PP.User();
                PP.gISWebVersion = bool.Parse(ConfigurationManager.AppSettings.Get("ISWebVersion"));
                PP.gServerIP = ConfigurationManager.AppSettings.Get("ServerIP");
                PP.gVersion = ConfigurationManager.AppSettings.Get("Version");

                textBox_server.Text = PP.gServerIP;

                if (PP.gISWebVersion == false)
                {
                    PP.gWorkFolder = Application.StartupPath;
                    PP.CreateFolder();//创建必要文件夹
                    PP.DeleteLogFile(PP.gWorkFolder + "\\Log", 10);
                    PP.gConfigFile = PP.gWorkFolder + "\\Config\\param.xml";
                    PP.gRoadFile = PP.gWorkFolder + "\\Config\\road.xml";
                    PP.gWFXWDMFile = PP.gWorkFolder + "\\Config\\WFXWDM.txt";
                    PP.gHPZLFile = PP.gWorkFolder + "\\Config\\HPZL.txt";
                    PP.gDPSDKWFDDFile = PP.gWorkFolder + "\\Config\\dpsdkwfdd.xml";
                    //if (!File.Exists(PP.gConfigFile)) PP.CreateDefaultXmlFile(PP.gConfigFile);
                    PP.ReadUserNameAndPassword();
                    this.comboBox_username.Text = PP.gUser.username;
                    this.comboBox_username.Enabled = false;
                }
                else
                {

                    PP.gWorkFolder = Application.StartupPath;
                    PP.CreateFolder();//创建必要文件夹
                    PP.DeleteLogFile(PP.gWorkFolder + "\\Log", 10);
                    PP.gConfigFile = PP.gWorkFolder + "\\WebConfig\\param.xml";
                    PP.gRoadFile = PP.gWorkFolder + "\\WebConfig\\road.xml";
                    PP.gWFXWDMFile = PP.gWorkFolder + "\\WebConfig\\WFXWDM.txt";
                    PP.gHPZLFile = PP.gWorkFolder + "\\WebConfig\\HPZL.txt";
                    PP.gDPSDKWFDDFile = PP.gWorkFolder + "\\WebConfig\\dpsdkwfdd.xml";
                    PP.gLuDuanFile = PP.gWorkFolder + "\\WebConfig\\luduan.xml";

                    try
                    {
                        if (PP.kdws == null)
                        {
                            PP.kdws = new JKZP.aitmskdws.aitmskdws();
                            PP.kdws.Url = "http://" + PP.gServerIP + "/aitmskdws/aitmskdws.asmx";
                            PP.kdws.Discover();
                        }

                        byte[] exe=null;
                        string serverVersion = "";
                        bool newVersionFound = PP.kdws.JKZP_HasNewVersion(PP.gVersion,out serverVersion, out exe);
                        if (newVersionFound == true)
                        {
                            if (exe != null)
                            {
                                File.WriteAllBytes(PP.gWorkFolder + "\\NEWJKZP.exe",exe);
                            }
                            MessageBox.Show("发现新版本抓拍软件Versioin="+serverVersion+"，请更新", "软件升级", MessageBoxButtons.OK);
                            PP.SetAppConfig("Version", serverVersion);
                            Process.Start(PP.gWorkFolder + "\\CSUpdate.exe");
                            Close();
                        }
                        else
                        {
                            PP.gUserXml = PP.kdws.JKZP_UserList();
                            if (PP.gUserXml != "")
                            {
                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml(PP.gUserXml);
                                XmlNodeList nodes = doc.SelectNodes("param/user");
                                if ((nodes != null) && (nodes.Count > 0))
                                {
                                    string[] userlist = new string[nodes.Count];
                                    for (int i = 0; i < nodes.Count; i++)
                                    {
                                        userlist[i] = nodes[i].Attributes["username"].Value;
                                    }
                                    comboBox_username.DataSource = userlist;
                                }
                            }
                            comboBox_username.Text = ConfigurationManager.AppSettings.Get("LastUserName");
                        }
                    }
                    catch (Exception er)
                    {
                        this.errorProvider1.SetError(this.textBox_server, "获取用户清单失败 " + er.Message);
                    }
                }
            }
            catch (Exception er)
            {
                this.errorProvider1.SetError(this.textBox_server, "LoginForm_Load()" + er.Message);
            }
            
        }

        private void button_login_Click(object sender, EventArgs e)
        {
            try
            {
                if (PP.gISWebVersion == false)
                {
                    if (textBox_password.Text == "")
                    {
                        this.errorProvider1.SetError(this.textBox_password, "请输入密码");
                    }
                    else if (textBox_password.Text != PP.gUser.password)
                    {
                        this.errorProvider1.SetError(this.textBox_password, "密码错误");
                    }
                    else
                    {
                        PP.LoadSysParam();//加载系统参
                        PP.gCameraTypeList = PP.LoadCamType();//读取摄像机类型
                        PP.gRoadList = PP.LoadRoadParam();//读取路口和摄像机
                        this.DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    if (comboBox_username.Text == "")
                    {
                        this.errorProvider1.SetError(this.comboBox_username, "请输入用户名");
                    }
                    else if (textBox_password.Text == "")
                    {
                        this.errorProvider1.SetError(this.textBox_password, "请输入密码");
                    }
                    else
                    {
                        
                        try
                        {
                            bool admin = false;
                            string xml = "";
                            string road = "";
                            string wfxwdm="";
                            string hpzl ="";
                            string error = "";
                            string dpsdkwfdd = "";
                            string luduan = "";
                            bool flag = PP.kdws.JKZP_Login(comboBox_username.Text, textBox_password.Text, out admin, out xml,out road,out wfxwdm,out hpzl, out error);
                            if (flag == false)
                            {
                                this.errorProvider1.SetError(this.textBox_password, "密码错误");
                            }
                            else
                            {
                                PP.gUser.username = comboBox_username.Text;
                                PP.gUser.password = textBox_password.Text;
                                PP.gUser.admin = admin;

                                flag = PP.kdws.JKZP_DPSDKGetWFDDDM(out dpsdkwfdd, out error);
                                luduan = PP.kdws.JKZP_GetLuDuan();

                                File.WriteAllText(PP.gConfigFile,xml);
                                File.WriteAllText(PP.gRoadFile, road);
                                File.WriteAllText(PP.gWFXWDMFile,wfxwdm);
                                File.WriteAllText(PP.gHPZLFile,hpzl);
                                File.WriteAllText(PP.gDPSDKWFDDFile, dpsdkwfdd);
                                File.WriteAllText(PP.gLuDuanFile, luduan);

                                if (PP.gUser.username != "")
                                {
                                    PP.SetAppConfig("LastUserName", PP.gUser.username);
                                }
                                PP.LoadSysParam();//加载系统参
                                PP.gCameraTypeList = PP.LoadCamType();//读取摄像机类型
                                PP.gRoadList = PP.LoadRoadParam();//读取路口和摄像机

                                //DPSDK
                                PP.gDPSDK_WFDD = PP.LoadDPSDKWFDD();
                                PP.gLuDuanList = PP.LoadLuDuan();

                                this.DialogResult = DialogResult.OK;
                            }
                        }
                        catch (Exception er)
                        {
                            this.errorProvider1.SetError(this.textBox_server, "无法连接到服务端" + er.Message);
                        }
                    }
                }
            }
            catch(Exception er) 
            {
                this.errorProvider1.SetError(this.textBox_password, "登陆失败："+er.Message);
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            if (PP.kdws != null) PP.kdws.Dispose();
            Close();
        }

        private void textBox_password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button_login_Click(sender, e);
            }
        }

        

        private void comboBox_username_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBox_username.Text != "")
            {
                PP.gUser.username = this.comboBox_username.Text;
            }
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (textBox_server.Text != "")
                {
                    PP.SetAppConfig("ServerIP", textBox_server.Text);
                }
            }
            catch { }
        }


    }
}
